package com.inspur.gs.sbssdl.sbssbxgl.sbssbx.sbssbx.back.bizactions;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.caf.db.dbaccess.DbParameter;
import com.inspur.edp.caf.db.dbaccess.IDbParameter;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import com.inspur.edp.common.commonudt.udt.attachmentinfo.attachmentinfo.entity.IAttachmentInfo;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import io.iec.edp.caf.boot.context.CAFContext;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class sbssbxscjstzdMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String dataid;
	private String jlz;
	public sbssbxscjstzdMgrAction(IBEManagerContext managerContext, String dataid, String jlz) {
		super(managerContext);
		this.dataid = dataid;
		this.jlz = jlz;
	}

	private IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

	public IBqlExecuter getBqlExecuter() {
		if (bqlExecuter == null) {
			bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);
		}
		return bqlExecuter;
	}

	private static final ObjectMapper objectMapper = new ObjectMapper();
	@Override
	public  void execute() {
		if (StringUtils.isNoneBlank(jlz)) {
			if (jlz.equals("Back")) {
				return;
			}
		}
		//创建session
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			//通过beid获取lcp
			String sbssbxid = "c5fd08f7-6410-4d7c-b662-693f94fde569";//物料需求计划实体id
			IStandardLcp bxlcp = iLcpFactory.createLcpByBEId(sbssbxid);
			IEntityData bxData = bxlcp.retrieve(dataid).getData();

			//todo 审批通过后推送至供应链平台，生成采购需求清单

			//获取主表字段数据
//			Object bxid = bxData.getID();//报修单id
//			Object sbid = ((IValuesContainer) bxData.getValue("sbid")).getValue("sbid");//设备id
//			Object sybmid = ((IValuesContainer) bxData.getValue("sybmid")).getValue("sybmid");//设备id
//
//
//			//主表新增
//			String jstzdid="a0cec7de-d3ef-40f8-9c19-e602f4a85586";
//			IStandardLcp jstzdlcp = iLcpFactory.createLcpByBEId(jstzdid);
//			IEntityData jstzddata=jstzdlcp.retrieveDefault();
//			ModifyChangeDetail curChange = new ModifyChangeDetail(jstzddata.getID());
//			((IValuesContainer)jstzddata.getValue("bxdid")).setValue("bxdid",bxid);
//			curChange.getPropertyChanges().put("bxdid",jstzddata.getValue("bxdid"));
//			((IValuesContainer)jstzddata.getValue("sbid")).setValue("sbid",sbid);
//			curChange.getPropertyChanges().put("sbid",jstzddata.getValue("sbid"));
//			((IValuesContainer)jstzddata.getValue("sybmid")).setValue("sybmid",sybmid);
//			curChange.getPropertyChanges().put("sybmid",jstzddata.getValue("sybmid"));
//			//获取备件子表数据
//			IEntityDataCollection bjxx = bxData.getChilds().get("bjxx");
//			for (IEntityData bjitem : bjxx) {
//				Object bjbh = bjitem.getValue("bjbh");//备件编号
//				Object bjmc = bjitem.getValue("bjmc");//备件名称
//				Object bjsl = bjitem.getValue("bjsl");//备件数量
//				Object bjjg = bjitem.getValue("bjjg");//备件价格
//				Object bjzj = bjitem.getValue("bjzj");//备件总价
//				Object bjly = bjitem.getValue("bjly");//备件来源
//
//				ArrayList<String> nodeCodes=new ArrayList<>();
//				nodeCodes.add("JSTZDBJ");
//				ArrayList<String> list=new ArrayList<>();
//				list.add(jstzddata.getID());
//				IEntityData bjdata=  jstzdlcp.retrieveDefaultChild(nodeCodes,list);
//				ModifyChangeDetail childChange=new ModifyChangeDetail(bjdata.getID());
//				childChange.getPropertyChanges().put("bjmc",bjmc);
//				childChange.getPropertyChanges().put("sl",bjsl);
//				curChange.addChildChangeSet("JSTZDBJ",childChange);
//			}
//
//			//获取外委厂家子表数据
//			IEntityDataCollection wwcjxx = bxData.getChilds().get("wwcj");
//			for (IEntityData wwcjitem : wwcjxx) {
//				Object cjbh = wwcjitem.getValue("cjbh");//厂家编号
//				Object cjmc = wwcjitem.getValue("cjmc");//厂家名称
//				Object dz = wwcjitem.getValue("dz");//地址
//				Object lxfs = wwcjitem.getValue("lxfs");//联系方式
//			}
//
//			//获取附件子表信息
//			IEntityDataCollection fjxx = bxData.getChilds().get("fj");
//			try {
//				for (IEntityData fjitem : fjxx) {
//					String serializeData = bxlcp.serializeData(fjitem);
//                    JsonNode jsonNode = objectMapper.readTree(serializeData);
//                    String fj = jsonNode.get("fj").toString();
//				}
//			} catch (JsonProcessingException e) {
//				throw new RuntimeException(e);
//			}

//			jstzdlcp.modify(curChange);
//			jstzdlcp.save();
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}
}